package com.fuhao.controller.portal;

import com.fuhao.base.BaseWebController;
import com.fuhao.constants.WebConstants;
import com.fuhao.core.base.BaseResponse;
import com.fuhao.dto.output.MemberOutDTO;
import com.fuhao.feign.MemberServiceFeign;
import com.fuhao.utils.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhang
 * @title: 首页
 */
@Controller
public class IndexController extends BaseWebController {

    private static final String INDEX = "index";
    @Autowired
    private MemberServiceFeign memberServiceFeign;

    @RequestMapping("/")
    public String index1(HttpServletRequest request, Model model){
        String token = CookieUtils.getCookieValue(request, WebConstants.LOGIN_TOKEN_COOKIENAME, true);
        if(!StringUtils.isEmpty(token)) {
            BaseResponse<MemberOutDTO> info = memberServiceFeign.getInfo(token);
            if (isSuccess(info)) {
                MemberOutDTO memberOutDTO = info.getData();
                if (memberOutDTO != null) {
                    String userName = memberOutDTO.getUserName();
                    model.addAttribute("username", userName);
                }
            }
        }
        return INDEX;
    }
}
